package com.lifesea.notify.mq.handler;

import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.lifesea.base.entity.PushAppEnums;
import com.lifesea.base.entity.PushConst;
import com.lifesea.base.entity.PushRequest;
import com.lifesea.commons.mq.entity.MqRequest;
import com.lifesea.commons.template.TemplateConst;
import com.lifesea.commons.utils.FreemarkerUtils;
import com.lifesea.commons.utils.PushUtils;
import com.lifesea.general.facade.TemplateFacade;
import com.lifesea.guahao.facade.GhOrderFacade;
import com.lifesea.model.TemplateInfo;
import com.lifesea.model.dto.AppPushRequest;
import com.lifesea.model.dto.GhOrderDto;
import com.lifesea.model.ext.GhOrderConst;
import com.lifesea.notify.service.IPushService;
import com.lifesea.rocketmq.utils.handler.MessageHandler;
import com.lifesea.rocketmq.utils.message.MessageDecoder;
import com.lifesea.rocketmq.utils.message.codec.JsonMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 停诊消息处理器
 */
@Component
public class BookStopSchedultNotifyHandler implements MessageHandler<MqRequest> {

    @Resource
    private IPushService pushService;
    @Autowired
    private GhOrderFacade ghOrderFacade;
    @Autowired
    private TemplateFacade templateFacade;

    private static final Logger logger = LoggerFactory.getLogger(BookStopSchedultNotifyHandler.class);

    @Override
    public ConsumeConcurrentlyStatus process(MqRequest data, MessageExt message, ConsumeConcurrentlyContext context) {
        logger.info("[停诊通知]接收,msgId={},notify={}", message.getMsgId(), data);

        try {

            List<GhOrderDto> list = ghOrderFacade.getDtoListBySchIdBookStatus(data.getBusinessId(), GhOrderConst.BOOK_STATUS_SUCCESS);
            AppPushRequest appPushRequest = new AppPushRequest();
            appPushRequest.setTemplateType(TemplateConst.TP_BOOK_STOP);

            TemplateInfo templateInfo = templateFacade.getTemplateByName(data.getTemplateType());
            for (GhOrderDto order : list) {
                String content = FreemarkerUtils.format(templateInfo.getTemplateText(), order);
                PushRequest pushRequest = new PushRequest();
                pushRequest.setAppcode(PushAppEnums.resident);
                pushRequest.setEcn(order.getEcn());
                pushRequest.setCategory(PushConst.PUSH_GUAHAO_DOCTOR_CLOSE);
                pushRequest.setBody(content);
                pushRequest.setRefId(String.valueOf(order.getBookId()));

                if (order.getEcn() != null) {
                    PushUtils.send(pushRequest);
                }
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } catch (Exception e) {
            logger.error("[停诊通知]处理失败", e);
        }
        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
    }

    @Override
    public MessageDecoder<MqRequest> getMessageDecoder() {
        return new JsonMessageDecoder<MqRequest>(MqRequest.class);
    }
}
